﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
using HF.WorkFlow.Const;
namespace HF.WorkFlow.Template
{
    /// <summary>
    /// 数据访问类WorkTask。
    /// </summary>
    public class WorkTask
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "WorkTaskId", "WorkFlowId", "TaskTypeId", "TaskTypeAndOr", "TaskCaption", "iXPosition", "iYPosition", "Description", "OperRule", "IsJumpSelf", "Identifying" };
        private string tableName = "HF_WorkTask";
        private string keyField = "WorkTaskId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(WorkTaskEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@WorkTaskId", entity.WorkTaskId, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@TaskTypeId", entity.TaskTypeId, typeof(string));
            sqlDataItem.AppendParameter("@TaskTypeAndOr", entity.TaskTypeAndOr, typeof(string));
            sqlDataItem.AppendParameter("@TaskCaption", entity.TaskCaption, typeof(string));
            sqlDataItem.AppendParameter("@iXPosition", entity.iXPosition, typeof(int));
            sqlDataItem.AppendParameter("@iYPosition", entity.iYPosition, typeof(int));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
            sqlDataItem.AppendParameter("@OperRule", entity.OperRule, typeof(string));
            sqlDataItem.AppendParameter("@IsJumpSelf", entity.IsJumpSelf, typeof(bool));
            sqlDataItem.AppendParameter("@Identifying", entity.Identifying, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public WorkTaskEntity GetWorkTaskEntity(DataRow dr)
        {
            WorkTaskEntity entity = new WorkTaskEntity();
            string value = "";
            entity.WorkTaskId = dr["WorkTaskId"].ToString();
            entity.WorkFlowId = dr["WorkFlowId"].ToString();
            entity.TaskTypeId =Convert.ToInt16(dr["TaskTypeId"]);
            entity.TaskTypeAndOr = dr["TaskTypeAndOr"].ToString();
            entity.TaskCaption = dr["TaskCaption"].ToString();
            value = dr["iXPosition"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.iXPosition = Convert.ToInt32(value);
            }
            value = dr["iYPosition"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.iYPosition = Convert.ToInt32(value);
            }
            entity.Description = dr["Description"].ToString();
            entity.OperRule = dr["OperRule"].ToString();
            value = dr["IsJumpSelf"].ToString();
            entity.Identifying = dr["Identifying"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.IsJumpSelf = true;
                }
                else
                {
                    entity.IsJumpSelf = false;
                }
            }
            return entity;
        }

        #endregion

        #region  构造函数
        public WorkTask()
        { }
        public static WorkTask GetInstance()
        {
            return new WorkTask();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(WorkTaskEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(WorkTaskEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string workTaskId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, workTaskId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="workTaskId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string workTaskId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="WorkTaskId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkTaskTable(string workTaskId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="workTaskId">主键Id</param>
        /// <returns>DataTable</returns>
        public WorkTaskEntity GetWorkTaskEntity(string workTaskId)
        {
            DataTable dt = GetWorkTaskTable(workTaskId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetWorkTaskEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法
        /// <summary>
        /// 删除任务的所以处理人
        /// </summary>
        /// <param name="workTaskId">任务Id</param>
        /// <returns>是否成功</returns>
        public bool DeleteOperatorOfTask(string workTaskId)
        {
            try
            {
                string sqlStr = "delete HF_Operator where  WorkTaskId=@worktaskId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@worktaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 删除任务的所有任务变量
        /// </summary>
        /// <param name="workTaskId">任务Id</param>
        /// <returns>是否成功</returns>
        public bool DeleteVarOfTask(string workTaskId)
        {
            try
            {
                string sqlStr = "delete HF_WorkTaskVar where  WorkTaskId=@worktaskId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@worktaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
                return true;


            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 删除任务的所有任务命令
        /// </summary>
        /// <param name="workTaskId">任务Id</param>
        /// <returns>是否成功</returns>
        public  bool DeleteCommandOfTask(string workTaskId)
        {
            try
            {
                string sqlStr = "delete HF_WorkTaskCommands where  WorkTaskId=@worktaskId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@worktaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 删除任务的所有表单
        /// </summary>
        /// <param name="workTaskId">任务Id</param>
        /// <returns>是否成功</returns>
        public bool DeleteFormsOfTask(string workTaskId)
        {
            try
            {
                string sqlStr = "delete HF_WorkTaskForm where  WorkTaskId=@worktaskId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@worktaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 删除任务的所有控制权限
        /// </summary>
        /// <param name="workTaskId">任务Id</param>
        /// <returns>是否成功</returns>
        public bool DeletePowerOfTask(string workTaskId)
        {
            try
            {
                string sqlStr = "delete HF_WorkTaskPower where  WorkTaskId=@worktaskId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@worktaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 删除任务模板,删除任务相关的所有信息
        /// </summary>
        /// <param name="workTaskId">workTaskId</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteWorkTaskTemplate(string workTaskId)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_DeleteWorkTask";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@workTaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程模板的所有任务节点
        /// </summary>
        /// <param name="workFlowId">流程Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetWorkTasks(string workFlowId)
        {
            try
            {
                string sqlStr = "select * from HF_WorkTask where WorkFlowId = @workflowId order by TaskCaption";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@workflowId", workFlowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程模板的所有任务节点
        /// </summary>
        /// <param name="workFlowId">流程Id</param>
        /// <returns>Entitys</returns>
        public List<WorkTaskEntity> GetWorkTaskEntitys(string workFlowId)
        {
            try
            {
                List<WorkTaskEntity> result = new List<WorkTaskEntity>();
                DataTable dt = GetWorkTasks(workFlowId);
                foreach (DataRow dr in dt.Rows)
                {
                    WorkTaskEntity entity = GetWorkTaskEntity(dr);
                    result.Add(entity);
                }
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程的开始节点，流程可以有多个开始节点
        /// </summary>
        /// <param name="workFlowId">流程模板Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetStartTasks(string workFlowId)
        {
            try
            {
                string sqlStr = "select * from HF_WorkTask where TaskTypeId =@TaskTypeId and workFlowId=@workFlowId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@TaskTypeId", WorkFlowConst.TaskType_Start,typeof(int));
                sqlItem.AppendParameter("@workFlowId", workFlowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得任务名称
        /// </summary>
        /// <param name="worktaskId">任务模板Id</param>
        /// <returns></returns>
        public  string GetTaskCaption(string workTaskId)
        {
            try
            {
                string sqlStr = "select TaskCaption from HF_WorkTask where WorkTaskId=@workTaskId";

                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@workTaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteScalar(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程与任务的视图
        /// </summary>
        /// <param name="worktaskId">任务模板Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkTaskView(string workTaskId)
        {
            try
            {
                string sqlStr = "select * from HF_WorkTaskView where WorkTaskId=@workTaskId";

                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@workTaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string GetWorkTaskUser(string workflowins)
        {
            try
            {
                string sqlStr = "SELECT	UserId FROM HF_WorkTaskInstanceView WHERE WorkFlowInstanceId=@workflowIns AND TaskTypeId='1'";

                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@workflowIns", workflowins);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteScalar(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }
        public string GetWorkTaskTables(string worktaskid)
        {
            try
            {
                string sqlStr = "select Identifying from HF_WorkTask where WorkTaskId=@workTaskId";

                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@workTaskId", worktaskid);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteScalar(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获得流程的所有任务节点
        /// </summary>
        /// <param name="workFlowId">流程Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkTasksView()
        {
            try
            {
                string sqlStr = "select * from HF_WorkTask";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

